import { Injectable } from '@angular/core';
import { delay, Observable, of, range } from 'rxjs';

import { fnRandomInt } from '@util/tools';

export interface User {
  id?: string;
  product?: string;
  billingMode?: string;
  automaticRenewal?: string;
  orderTime?: Date;
  usageTime?: string;
  status?: string;
  negotiatedAmount?: number;
  discount?: number;
  officialReceipts?: number;
  configInfo?: string;
}

/**
 * 模拟请求
 */
@Injectable({ providedIn: 'root' })
export class HttpMockService {
  constructor() {}

  getUser(userid: string): Observable<User> {
    console.log(`getUser(${userid})`);
    const loadTime = fnRandomInt(1, 1000);
    return of(this.create(userid)).pipe(delay(loadTime));
  }

  getUsers(): Observable<User[]> {
    console.log('getUsers');
    const users: User[] = [];
    range(1, 100).subscribe(id => users.push(this.create(`${id}`)));

    const loadTime = fnRandomInt(1000, 2000);
    return of(users).pipe(delay(loadTime));
  }

  private create(userid: string): User {
    return {
      id: userid,
      product: `Cloud Database - ${userid}`,
      billingMode: 'Prepaid',
      automaticRenewal: 'YES',
      orderTime: new Date(),
      usageTime: '2018-04-24 18:00:00 To 2019-04-24 18:00:00',
      status: 'Running',
      negotiatedAmount: 80,
      discount: 20,
      officialReceipts: 60,
      configInfo: 'Data disk type: MongoDB\nDatabase version: 3.4\nPackage: dds.mongo.mid\nStorage space: 10 GB\nReplication_factor:3\nRegion: East China 1'
    };
  }
}
